package xin.nick.bean;

import org.apache.dubbo.config.annotation.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import xin.nick.service.MyService;

import java.util.concurrent.*;

/**
 * @author Nick
 * @date 2021/7/17
 * @description
 */
@Component
public class ConsumerComponent {

    private static final Logger logger =
            LoggerFactory.getLogger(ConsumerComponent.class);

    private static ExecutorService threadPool = new ThreadPoolExecutor(
            6,
            240,
            12,
            TimeUnit.SECONDS,
            new LinkedBlockingDeque<>(6),
            Executors.defaultThreadFactory(),
            new ThreadPoolExecutor.AbortPolicy());

    @Reference
    private MyService myService;

    public void callMethod1() {
        threadPool.submit(() -> {
            while (true) {
                try {
                    Thread.sleep(25);
                    threadPool.submit(() -> {
                        myService.method1();
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void callMethod2() {
        threadPool.submit(() -> {
            while (true) {
                try {
                    Thread.sleep(25);
                    threadPool.submit(() -> {
                        myService.method2();
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void callMethod3() {
        threadPool.submit(() -> {
            while (true) {
                try {
                    Thread.sleep(25);
                    threadPool.submit(() -> {
                        myService.method3();
                    });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }


}
